package com.yuan.farmerwork.ynblog.rest.admin;

import com.yuan.farmerwork.ynblog.domain.Classify;
import com.yuan.farmerwork.ynblog.domain.Series;
import com.yuan.farmerwork.ynblog.domain.Tag;
import com.yuan.farmerwork.ynblog.domain.YnDocument;
import com.yuan.farmerwork.ynblog.domain.pojo.ClassifiesBo;
import com.yuan.farmerwork.ynblog.service.ClassifyService;
import com.yuan.farmerwork.ynblog.service.SeriesService;
import com.yuan.farmerwork.ynblog.service.TagService;
import com.yuan.farmerwork.ynblog.service.YnDocumentService;
import com.yuan.farmerwork.ynblog.utils.constants.CommConstants;
import com.yuan.farmerwork.ynblog.utils.constants.PageConstants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.servlet.ModelAndView;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.stream.Collectors;

/**
 * 配置页面控制器.
 *
 * @author yjs
 * @date 2021/1/14 15:23
 */
@Controller
public class ConfigureController {

    @Autowired
    private TagService tagService;
    @Autowired
    private SeriesService seriesService;
    @Autowired
    private ClassifyService classifyService;
    @Autowired
    private YnDocumentService documentService;

    /**
     * 添加标签，分类，系列,文档页面.
     *
     * @param map 页面参数map
     * @return
     */
    @GetMapping("/configure")
    public ModelAndView configure(Map map) {

        //获取标签参数
        List<ClassifiesBo> tagsBoList = new ArrayList<>();
        List<Tag> taglist = tagService.list();
        taglist.stream().map(x -> {
            ClassifiesBo classifiesBo = new ClassifiesBo();
            classifiesBo.setName(x.getTagName());
            classifiesBo.setColor(CommConstants.COLO_LIST.get(new Random().nextInt(CommConstants.COLO_LIST.size())));
            tagsBoList.add(classifiesBo);
            return x;
        }).collect(Collectors.toList());
        map.put(PageConstants.PAGE_PARAM_TAG_BO, tagsBoList);

        //获取分类参数
        List<ClassifiesBo> classifiesBoList = new ArrayList<>();
        List<Classify> classifList = classifyService.list();
        classifList.stream().map(x -> {
            ClassifiesBo classifiesBo = new ClassifiesBo();
            classifiesBo.setName(x.getClassifyName());
            classifiesBo.setColor(CommConstants.COLO_LIST.get(new Random().nextInt(CommConstants.COLO_LIST.size())));
            classifiesBoList.add(classifiesBo);
            return x;
        }).collect(Collectors.toList());
        map.put(PageConstants.PAGE_PARAM_CLASSIF_BO, classifiesBoList);

        //获取系列参数
        List<ClassifiesBo> secriscBoList = new ArrayList<>();
        List<Series> seriesList = seriesService.list();
        seriesList.stream().map(x -> {
            ClassifiesBo classifiesBo = new ClassifiesBo();
            classifiesBo.setName(x.getSericsName());
            classifiesBo.setColor(CommConstants.COLO_LIST.get(new Random().nextInt(CommConstants.COLO_LIST.size())));
            secriscBoList.add(classifiesBo);
            return x;
        }).collect(Collectors.toList());
        map.put(PageConstants.PAGE_PARAM_SECRISC_BO, secriscBoList);

        //获取文档参数
        List<ClassifiesBo> documentBoList = new ArrayList<>();
        List<YnDocument> documentList = documentService.list();
        documentList.stream().map(x -> {
            ClassifiesBo classifiesBo = new ClassifiesBo();
            classifiesBo.setName(x.getTitle());
            classifiesBo.setColor(CommConstants.COLO_LIST.get(new Random().nextInt(CommConstants.COLO_LIST.size())));
            documentBoList.add(classifiesBo);
            return x;
        }).collect(Collectors.toList());
        map.put(PageConstants.PAGE_PARAM_DOCUMENT_BO, documentBoList);

        return new ModelAndView(PageConstants.PAGE_CONFIGURE, map);
    }
}
